const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
const { ModuleFederationPlugin } = require("webpack").container;
const { generateRemote } = require("../../utils");
const {
  SanRemoteModulePlugin,
  ListenAssetsJsPlugin,
  ScanModuleOutFolder,
  updateAppInfo,
} = require("./plugins");
const deps = require("../package.json").dependencies;
const appFiles = require("../../appConfig.json");
delete appFiles.base;

console.log("process.env.NODE_ENV:", process.env.NODE_ENV);

module.exports = {
  entry: path.resolve(__dirname, "../src/index.tsx"),
  output: {
    clean: true,
    path: path.resolve(__dirname, "../dist"),
    filename: "[name].[contenthash:8].bundle.js",
    chunkFilename: "[name].[contenthash:8].chuck.js",
    // filename: "[name].[hash:8].js",
  },
  resolve: {
    extensions: [".jsx", ".js", ".ts", ".tsx"],
  },
  module: {
    rules: [
      {
        test: /\.[jt]sx?$/,
        include: [path.resolve(__dirname, "../src")],
        use: [
          {
            loader: "babel-loader",
            options: {
              rootMode: "upward",
            },
          },
        ],
      },
      {
        test: /\.less$/,
        include: [
          /node_modules[\\/]@qtrade/,
          path.resolve(__dirname, "../src"),
        ],
        exclude: [/\.module(s?)\.less$/],
        use: [
          "style-loader",
          "css-loader",
          {
            loader: "less-loader",
            options: {
              lessOptions: {
                javascriptEnabled: true,
              },
            },
          },
        ],
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, "../public/index.html"),
      ...appFiles,
    }),
    new ModuleFederationPlugin({
      name: "base",
      filename: "entry.[contenthash:8].js",
      exposes: {
        "./store": "./src/store",
      },
      shared: {
        ...deps,
        "./src/store": {},
      },
      remotes: {
        fl: generateRemote("fl", "."),
        "@qtrade/error-catch": generateRemote("fl", "./qtrade/error-catch"),
        common: generateRemote("common", "."),
        "@/utils": generateRemote("common", "./utils"),
        "@/components": generateRemote("common", "./components"),
        react: generateRemote("common", "./react"),
        "react-dom": generateRemote("common", "./react-dom"),
        "react-router-dom": generateRemote("common", "./react-router-dom"),
      },
    }),
    new SanRemoteModulePlugin(),
    new ScanModuleOutFolder({ absoluteFolders: path.resolve(__dirname, "..") }),
    new ListenAssetsJsPlugin((file) => {
      if (file.startsWith("entry")) {
        const fileContext = "/"; //"http://localhost:4000/"
        updateAppInfo("base", fileContext + file);
        return true;
      }
    }),
  ],
};
